package com.yan.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yan.domain.UserBank;
import com.yan.model.R;
import com.yan.service.UserBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userBanks")
@Api(tags = "会员的银行卡管理")
public class UserBankController {

    @Autowired
    private UserBankService userBankService;

    @GetMapping
    @ApiOperation(value = "分页查询用户的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usrId", value = "会员的Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(Page<UserBank> page, Long userId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<UserBank> userBankPage = userBankService.findByPage(page, userId);
        return R.ok(userBankPage);
    }

    @PostMapping("/status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡的Id"),
            @ApiImplicitParam(name = "status", value = "银行卡的状态"),
    })
    @ApiOperation(value = "修改银行卡的状态")
    public R updateStatus(Long userId, byte status) {
        UserBank userBank = new UserBank();
        userBank.setId(userId);
        userBank.setStatus(status);
        boolean updateById = userBankService.updateById(userBank);
        return updateById ? R.ok() : R.failed("修改银行卡状态失败");
    }

    @PatchMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank", value = "银行卡的json数据"),
    })
    @ApiOperation(value = "修改银行卡")
    public R updateStatus(@RequestBody @Validated UserBank userBank) {
        boolean save = userBankService.save(userBank);
        return save ? R.ok() : R.failed("修改状态失败");
    }

    @GetMapping("/current")
    @ApiOperation(value = "查询用户的卡号")
    public R<UserBank> currentUserBank() {
        String idStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        UserBank userBank = userBankService.getUserBankByUserId(Long.valueOf(idStr));
        return R.ok(userBank);
    }

    @PostMapping("/bind")
    @ApiOperation(value = "绑定银行卡")
    public R bindBank(@RequestBody @Validated UserBank userBank) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean ok = userBankService.bindBank(userId, userBank);
        return ok ? R.ok() : R.failed("绑定失败");
    }

}
